import java.util.Arrays;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int[][] Max = {{0,0,4,4},{2,7,5,0},{3,6,10,10},{0,9,8,4},{0,6,6,10}};//需要的总资源
        int[][] Allocation = {{0,0,3,2},{1,0,0,0},{1,3,5,4},{0,3,3,2},{0,0,1,4}};//已拥有资源
        int[][] need = new int[5][4];//还需资源
        int[] Available = {1,6,2,2};//系统资源
        int[] Request = {1,2,2,2};//p2向系统索要资源
        int px= 2;
        int[] _Allocation = new int[4];//临时资源
        int[] _Available = new int[4];//临时资源
        int[] _need = new int[4];//临时资源
        String _Allocations = "";//临时资源
        String _Availables = "";//临时资源
        String _needs = "";//临时资源
        int[] AvailableMax = Arrays.copyOf(Available,4);//把目前系统拥有资源给最大资源
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入Px进程：");
        px = scanner.nextInt();
        System.out.println("请输入要向系统请求的资源：");
        for (int i = 0; i < Request.length; i++) {
            Request[i] = scanner.nextInt();
        }
        for (int i = 0; i < 4; i++) {//计算系统总资源
            for (int j = 0; j < 5; j++) {
                AvailableMax[i] += Allocation[j][i] ;
            }
        }
        boolean[] state = {true,true,true,true,true};//5个进程的状态
        for (int i = 0; i < need.length; i++) {
            for (int j = 0; j < need[i].length; j++) {
                need[i][j] = Max[i][j]-Allocation[i][j];
            }
        }
        for (int i = 0; i < need[px].length; i++) {
            if(Request[i]<=need[px][i]){
                if(Request[i]<=Available[i]){
                    _Available[i] = Available[i] - Request[i];
                    _Allocation[i] = Allocation[px][i] + Request[i];
                    _need[i] = need[px][i] - Request[i];
                    _Availables+=_Available[i]+" ";
                    _Allocations+=_Allocation[i]+" ";
                    _needs +=_need[i]+" ";
                }else{
                    System.out.println("进程p2受阻");
                }
            }else{
                System.out.println("超出所需资源最大值");
            }
        }

        int count = 0;
        System.out.println("安全序列：");
        for (int i = 0; i < need.length; i++) {
            if(!state[i]){
                continue;
            }else{
                for (int j = 0; j < need[i].length; j++) {
                    if(i==px&&_need[j]<=_Available[j]){//等于进程px时，会和临时need比较
                        count++;
                    }else if(i!=px&&need[i][j]<=_Available[j]){
                        count++;
                    }
                }
                if(count==4){//count=4说明可以分配资源给p[i]
                    for (int j = 0; j < _Available.length; j++) {
                        if(i==px){
                            _Available[j] +=_Allocation[j];//进程用还并返还给系统
                        }else{
                            _Available[j] += Allocation[i][j];//进程用还并返还给系统
                        }
                    }
                    state[i] = false;
                    System.out.print("p"+i+" ");
                    i=-1;
                }
            }
            count=0;
        }
        System.out.println();
        count=0;
        for (int i = 0; i < _Available.length; i++) {
            if(_Available[i]==AvailableMax[i]){
                count++;
            }
        }
        if(count==4){
            System.out.println("系统可分配！");
            System.out.println("进程p"+px+",分配后的资源为[Allocation:"+_Allocations+",Need:"+_needs+",Available:"+_Availables+"]");
            for (int i = 0; i <need[px].length ; i++) {//返还真实资源
                need[px][i] = _need[i];
                Allocation[px][i] = _Allocation[i];
                Available[i] = _Available[i];
            }
        }  else{
            System.out.println("系统不可分配！");
        }
    }
}
